/*
 	Software Author: Mike Moore
 	Vectrics Flashrecruit
 	Open source job board and applicant tracking system.  Built with enterprise Java.
 	GPL License applies to this source code.
 */
package org.vectrics.struts;

import java.math.BigDecimal;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.vectrics.provider.CoreServiceLocator;

/**
 * @author mmoore
 */
public class VectricsActionForm extends ActionForm {

	private static final long serialVersionUID = 8287221518613375281L;

	public VectricsActionForm() {
		super();
	}

	protected boolean wasButtonPressed(Request request, String name) {
	    boolean result = false;
	    String paramValue = request.getParameter(name);
	    if (paramValue == null) {
	        paramValue = request.getParameter(name + ".x");
	    }
	    if (paramValue != null)
	        result = true;
	    return(result);
	}
	
	public BigDecimal stringToMoney(String string) {
		// Remove any comma's or dollar signs the user put in the money entry
		string = string.replaceAll(",", "");
		string = string.replaceAll("\\$", "");
		BigDecimal value = new BigDecimal(string);
		return(value);
	}
	
	public boolean isBlank(String str) {
		boolean result = false;
		if ((str == null) || (str.trim().length() == 0)) {
			result = true;
		}
		return(result);
	}
	
	protected ActionErrors doValidate(ActionMapping mapping, Request request) {
		ActionErrors errors = new ActionErrors();
		return(errors);
	}
	
	public final ActionErrors validate(ActionMapping mapping, HttpServletRequest servletRequest) {
		Request request = HttpRequestConverter.convertRequest(servletRequest);
		return(doValidate(mapping, request));
	}
	
	/**
	 * Returns the Locale specific phrase for the passed code.
	 * @param code The code that determines the phrase that will be used.
	 * @param locale The locale of the user.
	 * @return
	 */
	protected String getPhrase(String code, Locale locale) {
		return CoreServiceLocator.getPhraseProvider().getMessage(code, locale);
	}
	
}
